import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;


public class AnimationDemo extends JFrame{
	public AnimationDemo(){
		add(new MovingMessagePanel("message moving?"));
	}
	public static void main(String[] args){
		AnimationDemo frame = new AnimationDemo();
		frame.setTitle("AnimationDemo");
		frame.setSize(280, 100);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	static class MovingMessagePanel extends JPanel{
		private String message = "welcome to java";
		private int xCoordinate = 0;
		private int yCoordinate = 20;
		
		public MovingMessagePanel(String message){
			this.message = message;
			
			Timer timer = new Timer(100,new TimerListener());
			timer.start();
		}
		protected void paintComponent(Graphics g){
			super.paintComponents(g);
			
			if(xCoordinate>getWidth()){
				xCoordinate =  -20;
			}
			xCoordinate += 5;
			g.drawString(message, xCoordinate, yCoordinate);
		}
		class TimerListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				repaint();
			}
		}
	}
	
}
